package tpaagp.scheduleus;

import java.io.IOException;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.exina.android.calendar.CalendarActivity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class OptionsMenu extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.options_menu);

		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		populateList(prefs.getBoolean("notificationsEnabled", true));
		
		// TODO DISABLE OPTIONS IF AVAILABILITY NOT SET

		final ListView lv = (ListView) findViewById(R.id.optionsList);
		lv.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> myAdapter, View myView,int myItemInt, long mylng) {
				String selectedFromList = (String) (lv.getItemAtPosition(myItemInt));
				if (selectedFromList.compareTo("Send meeting request") == 0) {
					SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
					String user = prefs.getString("Name", "user");
					String urlS = prefs.getString("URL", "url");
					String queryString = "crear_meeting.php?user="+user+"&accion=crear";
					HttpClient client = new DefaultHttpClient();
			        HttpGet request = new HttpGet(urlS+queryString);
					try {
						client.execute(request);
					} catch (ClientProtocolException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
					//TODO [TOAST | Activity]
					
				} 
				else if (selectedFromList.compareTo("Specify your availability") == 0) {
					startSpecifyAvailability();
				} 
				else if (selectedFromList.compareTo("Show your workgroups") == 0) {
					SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
					String group = prefs.getString("workgroup", "workgroup");
					System.out.println(group);
					Toast.makeText(OptionsMenu.this, group, Toast.LENGTH_LONG).show();
				}
				else if (selectedFromList.compareTo("Show pending meetings") == 0) {
					startCalendarView();
				} 
				else if (selectedFromList.compareTo("Enable notifications") == 0) {
					// Create an alertbox and ask
					SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
					askForSure(prefs.getBoolean("notificationsEnabled", true));
				}
				else if (selectedFromList.compareTo("Disable notifications") == 0) {
					// Create an alertbox and ask
					SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
					askForSure(prefs.getBoolean("notificationsEnabled", true));
				}
			}
		});
	}

	private void startCalendarView() {
		Intent i = new Intent(Intent.ACTION_VIEW).setDataAndType(null,
				CalendarActivity.MIME_TYPE);
		try {
			startActivity(i);
		} catch (ActivityNotFoundException e) {
			System.out.println("Activity not found");
		}
	}

	private void startSpecifyAvailability() {
		// TODO Auto-generated method stub
		Intent i = new Intent(this, SpecifyAvailability.class);
		try {
			startActivity(i);
		} catch (ActivityNotFoundException e) {
			System.out.println("Activity not found");
		}
	}

	private void askForSure(boolean enabled) {

		AlertDialog.Builder a = new AlertDialog.Builder(this);
		final boolean en = enabled;
		if (en) {
			a.setMessage(R.string.disableNot).setPositiveButton("OK",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							populateList(!en);
						}
					});
		} else {
			a.setMessage(R.string.enableNot).setPositiveButton("OK",
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							populateList(!en);
						}
					});
		}
		a.show();
	}

	private void populateList(boolean enabled) {
		String[] options;
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
		SharedPreferences.Editor ed = prefs.edit();
		if (enabled) {
			ed.putBoolean("notificationsEnabled", true);
			if (!ed.commit()) {
				System.out.println("ERROR");
			}
			options = getResources().getStringArray(R.array.options_list_enabled);
		} else {
			ed.putBoolean("notificationsEnabled", false);
			if (!ed.commit()) {
				System.out.println("ERROR");
			}
			options = getResources().getStringArray(R.array.options_list_disabled);
		}

		ArrayAdapter<String> ad = new ArrayAdapter<String>(this, R.layout.options, options);
		ListView lv = (ListView) findViewById(R.id.optionsList);
		lv.setAdapter(ad);
	}
}
